package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.entity.Product;
import com.project.mapper.ProductMapper;
import com.project.service.ProductService;
import org.springframework.stereotype.Service;

/**
 * 商品业务接口实现类
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Override
    public IPage<Product> cutByItem(Integer pageNum, String productName, Integer startStoreNum, String type, Integer endStoreNum) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        if (productName != null && productName != ""){
            qw.like("p_name",productName);
        }
        if (startStoreNum != null){
            qw.ge("p_storeNum",startStoreNum);
        }
        if (endStoreNum != null){
            qw.le("p_storeNum",endStoreNum);
        }
        if (type != null && type != "") {
            qw.eq("p_type", type);
        }
        return this.baseMapper.selectPage(new Page<>(pageNum,3),qw);
    }

    @Override
    public Product findProductInfoById(Integer productId) {
        return this.baseMapper.selectById(productId);
    }

    @Override
    public void updateSellPrice(Integer productId, Integer newSellPrice) {
        Product product = this.baseMapper.selectById(productId);
        product.setSellPrice(newSellPrice);
        this.updateById(product);
    }
}
